#include "LuaScriptLogicProto.h"


namespace EntitySystem
{

class AttributeVisitor : public boost::static_visitor<>
{
private:    
    LuaScriptLogic *m_scriptLogic;
    std::string m_attributeName;
    
public:
    AttributeVisitor(LuaScriptLogic *scriptLogic, const std::string& attributeName)
    :
    m_scriptLogic(scriptLogic),
    m_attributeName(attributeName) 
    {
    }
    
    void operator()(int& value)
    {
	m_scriptLogic->PushNumber( m_attributeName, value );
    }
    
    void operator()(float& value)
    {
	m_scriptLogic->PushNumber( m_attributeName, value);
    }
    
    void operator()(bool& value)
    {
	m_scriptLogic->PushNumber( m_attributeName, value ? 1 : 0 );
    }
    
    void operator()(std::string& value)
    {
	m_scriptLogic->PushString( m_attributeName, value );
    }
    
    void operator()(Math::Vector2F& value)
    {
	Math::Vector2F *copy = new Math::Vector2F( value );
	
	m_scriptLogic->PushObject( m_attributeName, copy, "Math::Vector2Templ< float >*", true );
    }
};


LuaScriptLogicProto::LuaScriptLogicProto(const std::string &scriptPath)
:
m_scriptPath( scriptPath )
{
}


LuaScriptLogicProto::~LuaScriptLogicProto()
{
}


/**
 * Returns a LuaScriptLogic object based on this prototype.
 */ 

ScriptLogic* LuaScriptLogicProto::Clone()
{
    LuaScriptLogic* aClone = new LuaScriptLogic( m_scriptPath );
    
    AttributeMap::iterator iter( m_attributes.begin( ) ), end( m_attributes.end( ) );
    
    for( ; iter != end; ++iter )
    {
	AttributeVisitor visitor( aClone, iter->first );
	
	boost::apply_visitor( visitor, iter->second );
    }
    
    return aClone;
}


void LuaScriptLogicProto::SetParent(Entity* parent)
{
    ScriptLogic::SetParent( parent );
}


};